package com.ccxe.manage.web.customer;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ccxe.manage.filters.MdPwd;
import com.ccxe.manage.model.CustomerBean;
import com.ccxe.manage.model.UserBean;
import com.ccxe.manage.model.UserRoleBean;
import com.ccxe.manage.service.CustomerManager;
import com.ccxe.manage.service.UserManager;
import com.ccxe.manage.service.impl.CustomerManagerImpl;
import com.ccxe.manage.service.impl.RoleDaoService;
import com.ccxe.manage.service.impl.UserManageImpl;

public class UpdateServlet extends HttpServlet {

	private CustomerManager customerManager = new CustomerManagerImpl();

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

			this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//通过key判断操作
		String key = request.getParameter("key").trim();
		String customer_id = request.getParameter("cid");
		if (key.equals("toUp")&&customer_id!=null) {//跳转修改页面
			customer_id  = new String (customer_id.getBytes("iso-8859-1"),"utf-8");
			CustomerBean cb = customerManager.getById(customer_id);
			if (cb != null) {
				request.getSession().setAttribute("cus", cb);
				response.sendRedirect(request.getContextPath()+"/jsp/customer/upCustomer.jsp");
			} else {
				request.getRequestDispatcher("/servlet/SrhCus")
				.forward(request, response);
			}
		} else if (key.equals("doUp")) {//执行修改
			customer_id=request.getParameter("customerID");
			String company = request.getParameter("company");
			String contact_name = request.getParameter("contactName");
			String contact_phone = request.getParameter("contactPhone");
			String contact_fax = request.getParameter("contactFax");
			String contact_email = request.getParameter("contactEmail");
			String street = request.getParameter("street");
			String city = request.getParameter("city");
			String country = request.getParameter("country");
			String post_code = request.getParameter("postCode");
			// 赋值
			CustomerBean cb = new CustomerBean();
			cb.setCustomerID(customer_id);
			cb.setCompany(company);
			cb.setContactName(contact_name);
			cb.setContactPhone(contact_phone);
			cb.setContactFax(contact_fax);
			cb.setContactEmail(contact_email);
			cb.setStreet(street);
			cb.setCity(city);
			cb.setCountry(country);
			cb.setPostCode(post_code);
			//执行修改
			int result = customerManager.updateCustomer(cb);
			if (result > 0) {
				request.setAttribute("message", "修改客户:"+company+"成功!");
				request.getRequestDispatcher("/servlet/SrhCus")
				.forward(request, response);
			} else {
				request.setAttribute("message", "修改失败");
			}

		}
		if(key.equals("toUpUser")){
			UserManager userManager = new UserManageImpl();
			RoleDaoService roleDaoService = new RoleDaoService();
			String user_id = request.getParameter("userId");
			UserBean userBean=null;
			
			if (user_id != null) {
				user_id = new String(user_id.getBytes("iso-8859-1"), "utf-8");
				userBean = userManager.getByUid(user_id);
			}
			if (userBean != null) {
				request.getSession().setAttribute("user", userBean);
				request.getSession().setAttribute("roleList", roleDaoService.getByCondition());
				request.getRequestDispatcher("/jsp/customer/updateUser.jsp")
						.forward(request, response);
			} else {
				request.getRequestDispatcher("/servlet/SrhUser")
				.forward(request, response);
			}
		}
		if(key.equals("doUpUser")){
			UserManager userManager = new UserManageImpl();
			String userID = request.getParameter("userID");
			String userName = request.getParameter("userName");
			String userType = request.getParameter("userType");
			String userDisabled = request.getParameter("userDisable");
			String userEmail = request.getParameter("emailAddress");
			String emailNotifications = request.getParameter("emailN");
			String userRoleId = request.getParameter("userRole");
			String resourceUserRole = request.getParameter("resourceUserRole");
			boolean isUpdate = false;
			if(resourceUserRole != null && !"".equals(resourceUserRole.trim()))
				isUpdate = true;
			UserBean userBean = new UserBean();
			userBean.setUserID(userID);
			userBean.setUserName(userName);
			userBean.setUserType(userType);
			userBean.setUserDisable(userDisabled);
			userBean.setEmailAddress(userEmail);
			userBean.setEmailNotifications(emailNotifications);
			UserRoleBean urBean = new UserRoleBean();
			urBean.setRoleID(userRoleId);
			urBean.setUserID(userID);
			userBean.setUserRole(urBean);
			int result = userManager.updateUser(userBean,isUpdate);
			if (result>0) {
				request.setAttribute("message", "成功修改用户信息");
				request.getRequestDispatcher("/servlet/SrhUser")
				.forward(request, response);
			} else {
				request.setAttribute("message", "修改用户信息失败");
				request.getRequestDispatcher("/servlet/UpdateServlet?key=toUpUser&userId"+userID)
						.forward(request, response);
			}
		}
		if(key.equals("toUpPwd")){
			String userID = request.getParameter("userID");
			if(userID == null){
				request.getRequestDispatcher("/servlet/SrhUser")
				.forward(request, response);
			}else{
				userID=new String(userID.getBytes("iso-8859-1"), "utf-8");
				request.getSession().setAttribute("userID", userID);
				request.getRequestDispatcher("/jsp/customer/updateUserPassword.jsp")
				.forward(request, response);
			}
		}
		if(key.equals("doUpUserPwd")){
			UserManager userManager = new UserManageImpl();
			String userID = request.getParameter("userID");
			String rPassword = request.getParameter("rPassword");
			String rPasswordMD5 = MdPwd.getMD5(rPassword);
			String tPassword = request.getParameter("tPassword");
			String tPasswordMD5 = MdPwd.getMD5(tPassword);
			boolean flag = userManager.verifyPassword(userID, rPasswordMD5);
			if(flag){
				int result = userManager.updatePassword(userID, tPasswordMD5);
				if(result>0){
					request.setAttribute("message", "成功修改密码");
					request.getRequestDispatcher("/servlet/SrhUser")
					.forward(request, response);
				}else{
					request.setAttribute("message", "修改密码失败");
					request.getSession().setAttribute("userID", userID);
					request.getRequestDispatcher("/jsp/customer/updateUserPassword.jsp")
					.forward(request, response);
				}
			}else{
				request.setAttribute("message", "密码输入有误");
				request.getSession().setAttribute("userID", userID);
				request.getRequestDispatcher("/jsp/customer/updateUserPassword.jsp")
				.forward(request, response);
			}
			
		}
	}





}
